package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Position;
import com.ktgj.customer.domain.PositionDTO;
import com.ktgj.customer.domain.dto.PositionSelectDTO;
import com.ktgj.customer.domain.vo.PositionVO;
import com.ktgj.customer.service.IPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 职位Controller
 * @author ruoyi
 * @date 2021-07-27
 */
@ControllerAdvice
@RestController
@RequestMapping("/position")
public class PositionController extends BaseController {

    @Autowired
    private IPositionService positionService;

    /**
     * 查询职位列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Position position) {
        startPage();
        List<Position> list = positionService.selectPositionList(position);
        return getDataTable(list);
    }

    @PostMapping("/getPositionName")
    public TableDataInfo getPositionName(Position position) {
        List<Position> list = positionService.selectPositionList(position);
        return getDataTable(list);
    }

    /**
     * 导出职位列表
     */
    //@PreAuthorize(hasPermi = "system:position:export")
    @Log(title = "职位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Position position) throws IOException {
        List<Position> list = positionService.selectPositionList(position);
        ExcelUtil<Position> util = new ExcelUtil<Position>(Position.class);
        util.exportExcel(response, list, "职位数据");
    }

    /**
     * 获取职位详细信息
     */
    //@PreAuthorize(hasPermi = "system:position:query")
    @PostMapping("/podetial")
    public AjaxResult getInfo(@RequestBody Map<String, Object> map) {
        Long positionId = Long.valueOf(map.get("positionId").toString());
        return AjaxResult.success(positionService.selectPositionById(positionId));
    }

    /**
     * 新增职位
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PositionVO positionVO) {
        return toAjax(positionService.insertPosition(positionVO));
    }

    /**
     * 修改职位
     */
    //@PreAuthorize(hasPermi = "system:position:edit")
    @Log(title = "职位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PositionVO positionVO) {
        return toAjax(positionService.updatePosition(positionVO));
    }
    /**
     * 修改职位2
     */
    @PostMapping("/edit")
    public AjaxResult editPosition(@RequestBody PositionVO positionVO) {
        if (positionVO.getPositionId()==1) {
            return AjaxResult.error("管理员无法修改");
        }
        return toAjax(positionService.updatePosition(positionVO));
    }
    /**
     * 删除职位
     */
    //@PreAuthorize(hasPermi = "system:position:remove")
    @Log(title = "职位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{positionIds}")
    public AjaxResult remove(@PathVariable Long[] positionIds) {
        return toAjax(positionService.deletePositionByIds(positionIds));
    }

    @RequestMapping("/tree/{positionId}")
    public AjaxResult queryByIdMenu(@PathVariable("positionId") Long positionId) {
        PositionDTO positionDTO = positionService.queryMenuTree(positionId);
        return AjaxResult.success(positionDTO);
    }

    @PostMapping("getAll")
    public AjaxResult queryAll(@RequestBody Map<String,Object> map) {
        String dep = null;
        if (map.get("deptId") != null && !map.get("deptId").equals("")){
            dep = map.get("deptId").toString();
        }

        if(dep!=null && !dep.equals("")){
        PositionSelectDTO positionSelectDTO=new PositionSelectDTO();
        positionSelectDTO.setDeptId(new Long(dep));
        return AjaxResult.success(positionService.getAll(positionSelectDTO));
        }else{
            PositionSelectDTO positionSelectDTO=new PositionSelectDTO();
            return AjaxResult.success(positionService.getAll(positionSelectDTO));
        }
    }

    @PostMapping("queryPositionById")
    public AjaxResult queryPositionById(@RequestBody Map<String, Long> map) {
        return AjaxResult.success(positionService.queryPositionById(map.get("positionId")));
    }

    /**
     * 修改角色状态
     * */
    @PostMapping("/toggleStart")
    public AjaxResult toggleStart(@RequestBody PositionVO positionVO){
        int i = positionService.toggleStart(positionVO);
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }
}
